import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:medicine_reminder/utils/editlength.dart';
import '../Models/mlsWarn.dart';
import '../Models/conflits.dart';
import '../Models/fontSize.dart';

import '../Utils/DataManager.dart';

class Box extends StatefulWidget {
  Box({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _BoxState createState() => _BoxState();
}

class _BoxState extends State<Box> {
  var options = [];
  var name = "";

  Warn getDetail(name) {
    for (int i = 0; i < Warns.length; i++) {
      if (Warns[i].nameWithUnit == name) {
        return Warns[i];
      }
    }
    return null;
  }

  //模糊搜索
    String searchNames(String name) {
   print("search name " + name);
    var ans = List();
    int n = Warns.length;
    for (int i = 0; i < n; i++) {
      var cl = longest_common_sequence(Warns[i].name, name);
      var el = edit_length(Warns[i].name, name);
     if(Warns[i].name==name) {
       ans.add({ 'name': Warns[i].nameWithUnit});
     }
      ans.add({'cl': cl, 'el': el, 'name': Warns[i].nameWithUnit});
    }
    print("cal end");
    print(ans[0]['name']);
    return ans[0]['name'];
  }

  void getOptions() async {
    var res = searchNames(name);
     print('123456789');
    options = [];
    var tmp = res;
    options.add(tmp);
    print(options[0]+'第二次');
    setState(() {});
  }

  void nextPage(String selectedName) async {
    Warn detail = getDetail(selectedName);
    Navigator.pushNamed(context, "WarningPage", arguments: {"detail": detail})
        .then((val) {
      Navigator.pop(context, val);
    });
  }

  @override
  Widget build(BuildContext context) {
//    print(" select page" + context.toString());
    List<Widget> list = <Widget>[];

    if(this.options.length==1) {
      list.add(
        ListTile(
            title: Text(
              options[0],
              style: TextStyle(fontSize: oneSize),
            ),
            onTap: () {
              nextPage(options[0]);
            }),
      );
      list.add(Divider(
        height: 10.0,
        indent: 0.0,
        color: Colors.red,
      ));
    }

    return Scaffold(
      /*appBar: AppBar(
        centerTitle: true,
        title: Text(
          "选择药品",
          style: TextStyle(fontSize: oneSize),
        ),
      ),*/
      body: Column(
        children: <Widget>[
          ListTile(
            title: Text(
              "关键字",
              style: TextStyle(fontSize: oneSize),
            ),
            subtitle: TextField(
              autofocus: true, //是否自动获取焦点
              decoration: InputDecoration(
                hintText: this.name,
                prefixIcon: Icon(
                  Icons.search,
                  size: oneSize,
                ),
              ),
              onChanged: (val) {
                setState(() {
                  this.name = val;
                  getOptions();
                });
              },
              style: TextStyle(fontSize: oneSize),
            ),
          ),
          ListTile(
            title: Text(
              "可吃药品",
              style: TextStyle(fontSize: oneSize),
            ),
          ),
          Expanded(
            child: ListView(
              children: list,
            ),
          ),
        ],
      ),
      persistentFooterButtons: <Widget>[
        FlatButton(
          child: Text(
            "返回",
            style: TextStyle(fontSize: oneSize),
          ),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ],
    );
  }
}
